﻿using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using Library;

namespace DirectoryWeb
{
  /// <summary>
  /// プリンターリスト画面のフォームです。
  /// </summary>
  public partial class PrinterList : Page
  {
    #region イベントハンドラ
    protected void PrinterDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
      this.CountLabel.Text = String.Format("{0} 個のオブジェクト", ((ICollection)e.ReturnValue).Count);
    }

    protected void DetailDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
      if (this.PrinterListBox.SelectedIndex == -1)
      {
        e.Cancel = true;
      }
    }

    protected void DetailDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
      var printer = (PrintQueue)e.ReturnValue;
      ViewState["Speed"] = printer.Speed;
      ViewState["MaxResolution"] = printer.MaxResolution;
    }

    //印刷速度、最高解像度の値変更
    protected void TextBox_TextChanged(object sender, EventArgs e)
    {
      var txt = (TextBox)sender;
      var stateKey = txt.ID.Replace("TextBox", String.Empty);
      int value;

      if (Int32.TryParse(txt.Text, out value) == false)   //整数に変換できない時
      {
        decimal d;
        if (Decimal.TryParse(txt.Text, out d))  //実数には変換できる時
        {
          txt.Text = Decimal.ToInt32(Decimal.Truncate(d)).ToString();   //小数部を切捨て
        }
        else  //実数にも変換できない時
        {
          txt.Text = Convert.ToString(ViewState[stateKey]);   //変更前の値に戻す
          txt.Focus();
        }
      }
      ViewState[stateKey] = txt.Text;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
      Response.Redirect("Main.aspx?idx=4");
    }
    #endregion
  }
}